/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threadclient;

import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bryce
 */
public class ThreadClient implements Runnable {
    //Default values

    private static final int DEFAULT_PORT = 9090;
    private static final int DEFAULT_THREADS = 5;
    private static final int DEFAULT_CLIENTS_PER_THREAD = 1;
    private static final String DEFAULT_STRING = "This is a test of the emergency broadcasting system.";
    private static final int DEFAULT_CLIENT_SENDS = 20;
    private static final int DEFAULT_SLEEP_TIME = 300;
    //Variables
    int port, threads, clientsPer, numSends, clientId;
    String string, ip;
    ClientLogger logger;

    public ThreadClient() {
    }

    public ThreadClient(String IP, String port, String numThreads, String clientsPer, String numSends, String string, ClientLogger logger, int clientId) {
        this.ip = IP;
        this.logger = logger;
        this.clientId = clientId;
        if (port != null) {
            this.port = Integer.parseInt(port);
        } else {
            this.port = DEFAULT_PORT;
        }
        if (numThreads != null) {
            this.threads = Integer.parseInt(numThreads);
        } else {
            this.threads = DEFAULT_THREADS;
        }
        if (clientsPer != null) {
            this.clientsPer = Integer.parseInt(clientsPer);
        } else {
            this.clientsPer = DEFAULT_CLIENTS_PER_THREAD;
        }
        if (string != null) {
            this.string = string;
        } else {
            this.string = DEFAULT_STRING;
        }
        if (numSends != null) {
            this.numSends = Integer.parseInt(numSends);
        } else {
            this.numSends = DEFAULT_CLIENT_SENDS;
        }
    }

    public static void main(String[] args) {
        // Parse input params
        String arg1 = null, arg2 = null, arg3 = null, arg4 = null, arg5 = null, arg6 = null, arg7 = null;
        switch (args.length) {
            case 1:
                arg1 = args[0];
                break;
            case 2:
                arg1 = args[0];
                arg2 = args[1];
                break;
            case 3:
                arg1 = args[0];
                arg2 = args[1];
                arg3 = args[2];
                break;
            case 4:
                arg1 = args[0];
                arg2 = args[1];
                arg3 = args[2];
                arg4 = args[3];
                break;
            case 5:
                arg1 = args[0];
                arg2 = args[1];
                arg3 = args[2];
                arg4 = args[3];
                arg5 = args[4];
                break;
            case 6:
                arg1 = args[0];
                arg2 = args[1];
                arg3 = args[2];
                arg4 = args[3];
                arg5 = args[4];
                arg6 = args[5];
                break;
            case 7:
                arg1 = args[0];
                arg2 = args[1];
                arg3 = args[2];
                arg4 = args[3];
                arg5 = args[4];
                arg6 = args[5];
                arg7 = args[6];
                break;
        }
        // Start up the logger
        ClientLogger logger = new ClientLogger(arg6);
        Thread loggerThread = new Thread(logger);
        loggerThread.setDaemon(true);
        loggerThread.start();
        // Check how many threads we're creating.
        int mainThreads = DEFAULT_THREADS;
        if (arg3 != null) {
            mainThreads = Integer.parseInt(arg3);
        }
        // Create the specified number of threads.
        for (int i = 0; i < mainThreads; i++) {
            // Initialize the client
            ThreadClient threadClient = new ThreadClient(arg1, arg2, arg3, arg4, arg5, arg7, logger,i);
            Thread thread = new Thread(threadClient);
            thread.start();
            threadClient = null;//GC
        }
    }

    @Override
    public void run() {

        for (int i = 0; i < clientsPer; i++) {
            //Create client connections and complete sends one by one
            Client client = new Client(this.ip, this.port, this.string, this.numSends, this.logger, DEFAULT_SLEEP_TIME, clientId);
            client.establishConnection();
            try {
                Thread.sleep(DEFAULT_SLEEP_TIME);
            } catch (InterruptedException ex) {
                logger.addItem(new Date().getTime()+","+"connection failure");
            }
            client.performOperations();
            client = null;//GC
        }
    }
}
